<?php
namespace App\Services\Admin;

use App\Jobs\Fun;
use App\Repositories\Admin\CategoryRepository;
use App\Repositories\Admin\OrderBaleRepository;
use App\Repositories\Admin\OrderTempRepository;
use App\Repositories\Admin\CompanyRepository;
use Illuminate\Support\Facades\DB;

class OrderBaleService
{
    private $service;
    private $company;
    private $cate;

    public function __construct()
    {
        $this->service = new OrderBaleRepository();
        $this->company = new CompanyRepository();
        $this->cate = new CategoryRepository();
    }

    public function getIndex()
    {
        $models = $this->service->getIndexList();
        if (count($models)) {
            foreach ($models as $model) {
                $company = $this->company->getShowOneByUid($model->uid);
                $model->comName = $company ? $company->comName : '测试公司';
//                $model->periodTime = date('Y-m-d',$model->period);
                $model->periodTime = ($model&&$model->period>1500000000) ?
                    date('Y-m-d',$model->period) : '';
                $model->createTime = date('Y-m-d',$model->created_at);
            }
        }
        return $models;
    }

    /**
     * 获取web首页货单
     */
    public function getWebIndex()
    {
        $models = $this->service->getWebIndex();
        $dataArr = array();
        if (!count($models)) { return $dataArr; }
        foreach ($models as $key => $model) {
            $company = $this->company->getShowOneByUid($model->uid);
            $dataArr[$key]['comName'] = ($company&&$company->com_name) ?
                $company->com_name : '测试公司';
            $dataArr[$key]['baleName'] = $model->styleName;
            $dataArr[$key]['periodTime'] = ($model&&$model->period>1500000000) ?
                date('Y-m-d',$model->period) : '测试2019-01-01';
            $dataArr[$key]['img'] = $model->img ?
                $model->img : 'imgs/uploads/order.png';
            $dataArr[$key]['is_rob'] = $model->is_rob;
        }
        return $dataArr;
    }

    /**
     * 通过工厂ID，获取
     */
    public function getIndexByFac($uid)
    {
        $models = $this->service->getIndexByFacStatus($uid);
        if (!count($models)) { return array(); }
        $temp = new OrderTempRepository();
        $company = new CompanyRepository();
        $lastDay = strtotime(date('Ymd',time()).' 000000') ; //当前时间
        foreach ($models as $model) {
            //外发订单状态
            if ($temp->getShowOneByBale($model->id)) {
                $model->statusName = '抢单中';
            }
            else if ($model->created_at > $lastDay) {
                $model->statusName = '最新发布';
            }
            else if ($model->period < time()) {
                $model->statusName = '已过期';
            }
            else {
                $model->statusName = '已发布';
            }
            //有效期
            $model->periodName = date('Y-m-d',$model->period);
            //公司名称
            $comModel = $company->getShowOneByUid($model->uid);
            $model->comName = $comModel ? $comModel->name : '测试公司';
        }
        return $models;
    }

    /**
     * 通过公司ID，APP抢单列表获取
     */
    public function getIndexByCid($uid,$cateId)
    {
        $models = $this->service->getIndexByCid($uid,$cateId);
        if (!count($models)) { return array(); }
        foreach ($models as $model) {
            $model->periodStr = '有效至'.date('m月d日',$model->period);
        }
        return $models;
    }

    /**
     * 通过分类ID，获取订单
     */
    public function getOrderBaleByCid($cateId,$offset,$pageSize)
    {
      //  DB::enableQueryLog();
        $models = $this->service->getOrderBaleByCid($cateId,$offset,$pageSize);
      //  $sql=DB::getQueryLog();
        /// print_r($sql);exit;
        if (!count($models)) { return array(); }
        foreach ($models as $model) {
            $model->periodStr = '有效至'.date('m月d日',$model->period);
            $model->img=Fun::pImg($model->img);
        }
        return $models;
    }

//    /**
//     * 通过公司ID，获取
//     */
//    public function getIndexByUid($uid)
//    {
//        $models = $this->service->getIndexByUid($uid);
//        if (!count($models)) { return array(); }
//        //外发订单状态
//        $temp = new OrderTempRepository();
//        $lastDay = strtotime(date('Ymd',time()).' 000000') ; //当前时间
//        foreach ($models as $model) {
//            if ($temp->getShowOneByBale($model->id)) {
//                $model->statusName = '抢单中';
//            }
//            else if ($model->created_at > $lastDay) {
//                $model->statusName = '最新发布';
//            }
//            else if ($model->period < time()) {
//                $model->statusName = '已过期';
//            }
//            else {
//                $model->statusName = '已发布';
//            }
//            $model->periodName = date('Y-m-d H:i:s',$model->period);
//        }
//        return $models;
//    }

    /**
     * 通过公司ID，获取
     */
    public function getBalesByCid($uid,$page)
    {
        return $this->service->getAppBalesByCid($uid,$page);
    }

    public function add($data)
    {
        return $this->service->setAdd($data);
    }

    public function modify($data,$id)
    {
        return $this->service->setModify($data,$id);
    }

    public function getOne($id)
    {
        return $this->service->getShowOne($id);
    }

    public function getOneBycid($cid)
    {
        return $this->service->getShowOneByCid($cid);
    }

    /**
     * 货单分类列表
     */
    public function getCateList()
    {
        $models = $this->cate->getSubList(167);
        if ($models->isEmpty()) { return []; }
        return $models->toArray();
    }
}